package com.czl.module_storehouse.activity.remand.direct;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.czl.base.data.bean.tengyun.ProductBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.utils.PopWindowUtils;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.remand.damage.DirectDamageActivity;
import com.czl.module_storehouse.activity.remand.scan.RemandScanActivity;
import com.czl.module_storehouse.bean.RemandReturnBean;
import com.czl.module_storehouse.databinding.PopupDirectDamageBinding;
import com.czl.module_storehouse.event.DirectDamageEvent;
import com.czl.module_storehouse.mvp.presenter.ProductPresenter;
import com.czl.module_storehouse.mvp.presenter.RemandHomePresenter;
import com.czl.module_storehouse.mvp.presenter.StorehouseLocationPresenter;
import com.czl.module_storehouse.mvp.view.RemandHomeView;
import com.czl.module_storehouse.mvp.view.StorehouseLocationView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * @author xubin
 */
public class DirectReturnActivity extends RemandScanActivity implements RemandHomeView, StorehouseLocationView {

    private static final int TAG_DAMAGE = 1;
    private final RemandReturnBean mRemandReturnBean = new RemandReturnBean();
    @InjectPresenter
    RemandHomePresenter mRemandHomePresenter;
    @InjectPresenter
    StorehouseLocationPresenter mStorehouseLocationPresenter;
    @InjectPresenter
    ProductPresenter mProductPresenter;

    @Override
    protected ProductPresenter getProductPresenter() {
        return mProductPresenter;
    }

    @Override
    protected StorehouseLocationPresenter getLocationPresenter() {
        return mStorehouseLocationPresenter;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        binding.headerRemand.imageView.setVisibility(View.GONE);
        binding.headerRemand.tvTitleNum.setVisibility(View.GONE);
        binding.textDamage.setVisibility(View.VISIBLE);
        SpanUtils.with(binding.textDamage)
                .append("提示：请先归还完好物品，然后进行")
                .append("损坏登记")
                .setClickSpan(ContextCompat.getColor(getContext(), R.color.color_25A468), false,
                        v -> nextDamageSearchActivity())
                .create();
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(DirectDamageEvent event) {
        if (event != null) {
            finish();
        }
    }

    @Override
    protected boolean isTextEnable() {
        return true;
    }

    private void nextDamageSearchActivity() {
        List<ProductBean> productBeans = mAdapter.getlocatProductList();
        if (productBeans.size() >= 1) {
            showPopup();
        } else {
            nextDamageActivity();
        }
    }

    private void nextDamageActivity() {
        startActivity(new Intent(getContext(), DirectDamageActivity.class));
//        startActivity(new Intent(mContext, DirectDamageProductActivity.class));
    }

    private void showPopup() {
        PopupDirectDamageBinding binding = PopupDirectDamageBinding.inflate(getLayoutInflater());
        binding.textTitle.setText("是否归还物品然后再进行损坏登记？");
        int width = ScreenUtils.getScreenWidth() - SizeUtils.dp2px(30);
        PopWindowUtils.getInstance().createPopWindow(getContext(), binding.getRoot(), width, "提示");
        PopWindowUtils.getInstance().showBasePopupWindow(this.binding.getRoot());

        binding.textConfirm.setOnClickListener(v -> {
            PopWindowUtils.getInstance().dismissPopupWindow();
            submit(TAG_DAMAGE);
        });
        binding.textCancel.setOnClickListener(v -> PopWindowUtils.getInstance().dismissPopupWindow());
    }

    @Override
    protected void submit(int requestTag) {
        mRemandReturnBean.setLocatList(mAdapter.getlocatList());
        if (mRemandHomePresenter != null) {
            mRemandHomePresenter.directReturn(mRemandReturnBean, requestTag);
        }
    }

    @Override
    public <T> void showHttpResponse(HttpResponse<T> httpResponse) {
        super.showHttpResponse(httpResponse);
        if (RemandHomePresenter.TAG_REMAND_DIRECT_RETURN.equals(httpResponse.getRequestTag())) {
            showToast("归还成功");
            EventBus.getDefault().postSticky(new DirectDamageEvent());
            finish();
        }
    }
}
